<?php
/**
 * Created by PhpStorm.
 * User: chenxiaogang
 * Date: 2017/11/17
 * Time: 上午11:51
 */

namespace User\Model;


use Common\Model\CommonModel;

class ReportCurrentUsers extends CommonModel
{
    protected $connection = 'DB_CONFIG2';
    protected $trueTableName = 'report_current_users';

    /**
     * @use          [用户当前财务信息统计和用户当前订单统计 - mysql事件调存储过程统计得出每天一次]
     * @author       chenxiaogang
     * @param null $where
     * @param      $limit
     * @return mixed
     */
    public function report_current_orders_and_users($where = null, $limit)
    {
        $fields = 'ru.user_id,ru.channel_code,ru.all_loan_count,ru.all_loan_amt,ru.all_overdue_count,ru.all_overdue_amt,ru.all_renewal_count,ru.all_renewal_amt,ru.after_loan_officer';
        $fields .= ',ro.order_id,ro.current_loan_days,ro.current_loan_amt,ro.current_order_status,ro.current_enddate';
        $data = $this->alias( 'ru' )
            ->field( $fields )
            ->join( 'report_current_orders ro on ro.user_id=ru.user_id', 'left' )
            //->join( 'system_user u on ru.user_id=u.id', 'left' )
            ->where( $where )
            ->limit( $limit )
            ->select();
        return $data;
    }

    public function report_current_orders_and_users_count($where)
    {
        $data = $this->alias( 'ru' )
            ->join( 'report_current_orders ro on ro.user_id=ru.user_id', 'left' )
            //->join( 'system_user u on ru.user_id=u.id', 'left' )
            ->where( $where )
            ->count( 'ru.id' );
        return $data;
    }

}